<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Params
 *
 * @author ThaiDV
 */
class Common {
	/**
	 * Get a part of paragraph (summary)
	 * @param String $content  source paragraph
	 * @param Nummeric $charCount  number characters want to get
	 * @return String  a part of paragraph with length equavalent $charCount.
	 * @author LamNX
	 */
	public static function getSummary($content, $charCount=0) {
		$pos = 0;
		if(strlen($content) > $charCount) {
			$sentenceSymbol = array(".","!","?"," "); // điểm kết thúc câu
			$content = strip_tags($content, "<i></i>"); // những tag muốn giữ lại

			for ($i=$charCount; $i>0; $i--) {
				$ch = substr($content,$i,1);

				if (in_array($ch,$sentenceSymbol)) {
					$pos = $i;
					$i = 0;
				}
			}

			$temp = substr($content, 0, $pos = $pos > 0 ? $pos+1 : $charCount);

			return $temp . '...';
		} else return $content;
	}


	/**
	 * Get a part of username (login_id)
	 * @param String $content  source username
	 * @param Nummeric $charCount  number characters want to get
	 * @return String  a part of paragraph with length equavalent $charCount.
	 * @author ThaiDV
	 */
	public static function subUsername($content, $charCount=0) {
		if(strlen($content) > $charCount) {
			$temp = substr($content, 0, $charCount - 3);
			return $temp . '...';
		} else return $content;
	}

	/**
	 * Get a part of paragraph (summary) in UTF-8 encode
	 * @param String $content  source paragraph
	 * @param Nummeric $charCount  number characters want to get
	 * @return String  a part of paragraph with length equavalent $charCount.
	 * @author ThaiDV
	 */
	public static function subVNText($content, $charCount=0) {
		$pos = 0;
		if(mb_strlen($content, 'UTF-8') > $charCount) {
			$sentenceSymbol = array(".","!","?"," "); // điểm kết thúc câu
			$content = strip_tags($content, "<i></i>"); // những tag muốn giữ lại

			for ($i=$charCount; $i>0; $i--) {
				$ch = mb_substr($content,$i,1,'UTF-8');

				if (in_array($ch,$sentenceSymbol)) {
					$pos = $i;
					$i = 0;
				}
			}

			$temp = mb_substr($content, 0, $pos = $pos > 0 ? $pos+1 : $charCount,'UTF-8');

			return $temp . '...';
		} else return $content;
	}

	public static function calculateTimeAgo($time) {
		if($time) {
			switch ($time) {
				case time()-strtotime($time) >= 24*60*60:
					return date('d/m/Y H:m', strtotime($time));
				case time()-strtotime($time) >= 60*60:
					return date('G', time()-strtotime($time)) . ' giờ ' . intval(date('i', time()-strtotime($time))) . ' phút trước';
				case time()-strtotime($time) >= 60:
					return intval(date('i', time()-strtotime($time))) . ' phút ' .  intval(date('s', time()-strtotime($time))) . ' giây trước';
				default:
					return intval(date('s', time()-strtotime($time))) . ' giây trước';
			}
		}

		return '';
	}

	/**
	 * Return the unmark Vietnamese
	 * @param string $stringVN  Vietnamese with mark in utf8-charset
	 * @return string had removed mark
	 */
	public static function removeMarkInVn($stringVN) {
		$markVN=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
            "ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề"
            ,"ế","ệ","ể","ễ",
            "ì","í","ị","ỉ","ĩ",
            "ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
            ,"ờ","ớ","ợ","ở","ỡ",
            "ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
            "ỳ","ý","ỵ","ỷ","ỹ",
            "đ",
            "À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
            ,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
            "È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
            "Ì","Í","Ị","Ỉ","Ĩ",
            "Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
            ,"Ờ","Ớ","Ợ","Ở","Ỡ",
            "Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
            "Ỳ","Ý","Ỵ","Ỷ","Ỹ",
            "Đ");

            $unMark=array("a","a","a","a","a","a","a","a","a","a","a"
            ,"a","a","a","a","a","a",
            "e","e","e","e","e","e","e","e","e","e","e",
            "i","i","i","i","i",
            "o","o","o","o","o","o","o","o","o","o","o","o"
            ,"o","o","o","o","o",
            "u","u","u","u","u","u","u","u","u","u","u",
            "y","y","y","y","y",
            "d",
            "A","A","A","A","A","A","A","A","A","A","A","A"
            ,"A","A","A","A","A",
            "E","E","E","E","E","E","E","E","E","E","E",
            "I","I","I","I","I",
            "O","O","O","O","O","O","O","O","O","O","O","O"
            ,"O","O","O","O","O",
            "U","U","U","U","U","U","U","U","U","U","U",
            "Y","Y","Y","Y","Y",
            "D");

            return str_replace($markVN, $unMark, $stringVN);
	}

	public static function setTagFontSize($size) {
		switch($size) {
			case $size < 2:
				return 'fontSize9';
			case $size < 10:
				return 'fontSize10';
			case $size < 20:
				return 'fontSize12';
			case $size < 35:
				return 'fontSize13';
			case $size < 50:
				return 'fontSize14';
			case $size < 80:
				return 'fontSize16';
			case $size < 115:
				return 'fontSize17';
			case $size < 150:
				return 'fontSize18';
			case $size < 200:
				return 'fontSize19';
			case $size < 300:
				return 'fontSize20';
			default:
				return 'fontSize9';
		}
	}
	/**
	 * resize image
	 */
	public static function resizeImage($imagePath, $imageThumbPath = '', $newWidth) {
		list($width, $height) = getimagesize(Yii::getPathOfAlias('webroot').$imagePath);
		$newHeight = round($height * ($newWidth / $width));
		$image = new ImageResizer(Yii::getPathOfAlias('webroot') . $imagePath);
		$resizedImage = $image->resize($newWidth, $newHeight, Yii::getPathOfAlias('webroot') . $imageThumbPath);
	}
}
